package com.sun.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用的结果返回封装
 * 定制返回值解析器
 */
@Configuration
public class CommonReturnValue implements InitializingBean {

    /**
     * SpringMvc的内置组件
     */
    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @Override
    public void afterPropertiesSet() throws Exception {
        //获取参数解析器
        List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
        //获取返回值解析器
        List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> newHandlerList = new ArrayList<>(returnValueHandlers.size());


        returnValueHandlers.forEach(x -> {
            if (x instanceof RequestResponseBodyMethodProcessor) {
                newHandlerList.add(new MyHandlerMethodReturnValueHandler(x));
            }else{
                newHandlerList.add(x);
            }
        });

        requestMappingHandlerAdapter.setReturnValueHandlers(newHandlerList);
    }


    /**
     * springmvc–--->
     *  todo: RequestResponseBodyMethodProcessor处理@RequestBody注解和@ResponseBody注解
     */


    /**
     * 源码
     * WebMvcConfigurationSupport.java
     *
     * @Bean
     *        public RequestMappingHandlerAdapter requestMappingHandlerAdapter(
     *            @Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
     *            @Qualifier("mvcConversionService") FormattingConversionService conversionService,
     *            @Qualifier("mvcValidator") Validator validator) {
     *
     * 		RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
     * 		adapter.setContentNegotiationManager(contentNegotiationManager);
     * 		//WebMvcConfigurer接口实现类的configureMessageConverters，消息转换器
     * 		adapter.setMessageConverters(getMessageConverters());
     * 		adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer(conversionService, validator));
     * 		//WebMvcConfigurer接口实现类的addArgumentResolvers，定制参数解析器
     * 		adapter.setCustomArgumentResolvers(getArgumentResolvers());
     * 		//WebMvcConfigurer接口实现类的addReturnValueHandlers，定制返回值解析器
     * 		adapter.setCustomReturnValueHandlers(getReturnValueHandlers());
     *
     * 		if (jackson2Present) {
     * 			adapter.setRequestBodyAdvice(Collections.singletonList(new JsonViewRequestBodyAdvice()));
     * 			adapter.setResponseBodyAdvice(Collections.singletonList(new JsonViewResponseBodyAdvice()));
     *        }
     *
     * 		AsyncSupportConfigurer configurer = getAsyncSupportConfigurer();
     * 		if (configurer.getTaskExecutor() != null) {
     * 			adapter.setTaskExecutor(configurer.getTaskExecutor());
     *        }
     * 		if (configurer.getTimeout() != null) {
     * 			adapter.setAsyncRequestTimeout(configurer.getTimeout());
     *        }
     * 		adapter.setCallableInterceptors(configurer.getCallableInterceptors());
     * 		adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());
     *
     * 		return adapter;
     *    }
     */
}
